var MongoClient = require('mongodb').MongoClient;
var async = require('async');
var driver = require('./db');
var utils = require('./utils');


const Url = "mongodb://localhost:27017/manhua";

const table = 'favors';


var favorsDao = {};

// 添加
favorsDao.add = function (param, userId, res) {

    var _query = {
        "userId": require('mongodb').ObjectID(userId),
        "manhuaId": require('mongodb').ObjectID(param['_id'])
    };



    var data = {
        "title": param.title,
        "img": param.img,
        "userId": require('mongodb').ObjectID(userId),
        "manhuaId": require('mongodb').ObjectID(param['_id']),
        "readChapter": param.readChapter,
        "readChapterHref": param.readChapterHref,
        "chapterLength": param.chapterLength,
        "isNew": false,
        "readTime": new Date()
    };

    async.waterfall([
        // 连接数据库        
        function (callback) {
            driver.connect(callback, Url);
        },
        // 超找
        function (db, callback) {

            driver.find(db, callback, table, _query);
        },
        // 插入
        function (db, res, callback) {

            if (res.data.length == 0) {

                driver.insert(db, callback, table, data);

            } else {
                callback({ success: false, msg: '该漫画已经收藏了' }, db, res);
            }


        },
        function (db, res, callback) {
            driver.close(db, res, callback);
        }],
        // result
        function (err, result) {
            console.log("resulte");

            if (err) {
                res.json(err);
                return;
            }
            res.json(result);
        }
    );
};

// 删除
favorsDao.del = function (param, userId, res) {

    var _query = {
        "_id": require('mongodb').ObjectID(param['_id'])
    };

    async.waterfall([
        // 连接数据库        
        function (callback) {
            driver.connect(callback, Url);
        },
        // 查询
        function (db, callback) {

            driver.find(db, callback, table, _query);
        },
        // 删除
        function (db, res, callback) {

            if (res.data.length > 0) {
                driver.delete(db, callback, table, _query);
            } else {
                callback({ success: false, msg: '该漫画已经取消收藏' }, db, res);
            }


        },
        function (db, res, callback) {
            driver.close(db, res, callback);
        }],
        // result
        function (err, result) {
            console.log("resulte");

            if (err) {
                res.json(err);
                return;
            }
            res.json(result);
        }
    );
};

// 查询
favorsDao.search = function (param, userId, res) {

    var _query = {
        "userId": require('mongodb').ObjectID(userId)
    };
    if (param['manhuaId']) {
        _query['manhuaId'] = require('mongodb').ObjectID(param['manhuaId'])
    }

    console.log(_query);
    async.waterfall([
        // 连接数据库        
        function (callback) {
            driver.connect(callback, Url);
        },
        // 查询
        function (db, callback) {

            driver.find(db, callback, table, _query);
        },
        function (db, res, callback) {
            driver.close(db, res, callback);
        }],
        // result
        function (err, result) {
            console.log("resulte");

            if (err) {
                res.json(err);
                return;
            }
            res.json(result);
        }
    );
};

// 修改
favorsDao.update = function (param, userId, res) {

    var _query = {
        "userId": require('mongodb').ObjectID(userId),
        "manhuaId": require('mongodb').ObjectID(param['_id'])
    };

    var data = {};

    // 是否更新有推送new
    if (param['isNew']) {
        data['isNew'] = param['isNew'] ==="true"?true:false;
    }

    if (param['readChapter']) {
        data['readChapter'] = param.readChapter;
        data["readChapterHref"] = param.readChapterHref;
        data["readTime"] = new Date();
    }

    async.waterfall([
        // 连接数据库        
        function (callback) {
            driver.connect(callback, Url);
        },
        // 超找
        function (db, callback) {

            driver.find(db, callback, table, _query);
        },
        // 插入
        function (db, res, callback) {

            if (res.data.length == 0) {
                callback({ success: false, msg: '该漫画已经取消收藏了' }, db, res);
            } else {
                driver.update(db, callback, table, _query, {"$set":data});
            }

        },
        function (db, res, callback) {
            driver.close(db, res, callback);
        }],
        // result
        function (err, result) {
            console.log("resulte");

            if (err) {
                res.json(err);
                return;
            }
            res.json(result);
        }
    );
};

// 分页
favorsDao.page = function(param, userId, res){
    var _query = {
        "userId": require('mongodb').ObjectID(userId)
    };

    var page_size = Number(param.size) || 10;

    var page_cur = Number(param.curPage) || 1;

    async.waterfall([
        // 连接数据库        
        function (callback) {
            driver.connect(callback, Url);
        },
        // 查询
        function (db, callback) {
            driver.page(db, callback, table, _query, {}, page_cur, page_size, {readTime: -1});
            // driver.find(db, callback, table, _query);
        },
        function (db, res, callback) {
            driver.close(db, res, callback);
        }],
        // result
        function (err, result) {
            console.log("resulte");

            if (err) {
                res.json(err);
                return;
            }
            res.json(result);
        }
    );
};
module.exports = favorsDao;